<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Checklist\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Checklist\Model\TaskEntity;
use Checklist\Form\TaskForm;

class TaskController extends \Zend\Mvc\Controller\AbstractActionController
{
    public function indexAction() 
    { 
        $mapper = $this->getTaskMapper();
        return new ViewModel(array('tasks' => $mapper->fetchAll()));
    }
    
    public function addAction()
    { 
        $form = new TaskForm();
        $task = new TaskEntity();
        $form->bind($task);
        $request = $this->getRequest();
        if($request->isPost())
        {
            $form->setData($request->getPost());
            if($form->isValid())
            {                
                //save Task
                $this->getTaskMapper()->saveTask($task);
                // Redirect to list of tasks
                return $this->redirect()->toRoute('task');                
            }
            else
            { 
                $model = new ViewModel(array('error' => true , 'form' => $form));
                $model->setTemplate('checklist/task/add');
                return $model;
            }
        }
        return array(
            'form' => $form
        );
    }  
    
    public function editAction()
    {
        $id = (int)$this->params('id');
        if(!id)
        {
            return $this->redirect()->toRoute('task' , array('action' => 'index'));
        }
        
        $task = $this->getTaskMapper()->getTask($id);
        $form = new TaskForm();
        $form->bind($task);
        $request = $this->getRequest();
        if($request->isPost())
        {
            $form->setData($request->getPost());
            if($form->isValid())
            {
                $this->getTaskMapper()->saveTask($task);
                return $this->redirect()->toRoute('task');
            }
        }        
        return array(
            'id' => $id ,
            'form' => $form
        );
    }

    public function deleteAction()
    {
        $id = $this->params('id');
        $task = $this->getTaskMapper()->getTask($id); 
        if(!$task)
        {
            return $this->redirect()->toRoute('task');
        }
        
        $request = $this->getRequest();
        if($request->isPost())
        { 
            if($request->getPost()->get('del') == 'Yes')
            {
                $this->getTaskMapper()->deleteTask($id);
            }
            return $this->redirect()->toRoute('task');
        }
        return array(
            'id' => $id ,
            'task' => $task
        );
    }

    public function getTaskMapper()
    {
        $sm = $this->getServiceLocator();
        return $sm->get('TaskMapper');
    }
}